<?php
/**
 * 
公共类处理函数
 * @author jqrr
 *
 */
class publicAction extends Action
{
	function top()
	{
		global $_SC;
		$myuser=new USERDB();
        $cmr=new Model();
		$classsys=A("classsys");
        
		/***********************************************************************
        *自动获取当前出发地
        ***********************************************************************/
        //获取所有出发地城市
		$chufalist=$classsys->getchufadi();
        //尝试解析COOKIE中设置的当前出发地
        $now_from_dest = '';
        if(!empty($_COOKIE['from_dest'])) {
            foreach ($chufalist as $v) {
                if($v["classsys0"]==$_COOKIE['from_dest']) {
                    $now_from_dest = $v;
                    break;
                }
            }
        }
        //TODO 尝试获取用户常用出发地列表设置
        else {
            $uid = $_COOKIE["uchome_uid"];
            $uchome_auth = $_COOKIE["uchome_auth"];
            $choosechufa = $cmr->table(UHDQ."userchuafadi U")
                               ->join(DQ."classsys F on U.chuafaid = F.classsys0")
                               ->where("U.uid=".$uid)
                               ->select();
			if( strlen($uchome_auth)>5 && !empty($choosechufa))
                $now_from_dest = $choosechufa[0];
		}
        //以上都没有获取到出发地信息，则通过IP识别
        if(empty($now_from_dest)) {
            import("ORG.Net.IpLocation");
            $loc = new IpLocation();
            $userloc = $loc->getlocation('202.98.192.168');
            $user_country = iconv( "gbk" , "UTF-8//IGNORE", $userloc['country'] );
            $user_area = iconv( "gbk" , "UTF-8//IGNORE", $userloc['area'] );
            //TODO debug
            //echo $user_country.$user_area;
            $now_from_dest = array(
                'classsys0' => '1815',
                'classsys1' => '南昌',
                'classsys2' => '0',
                'classsys3' => '542',
                'classsys4' => '7',
                'classsys5' => '0',
                'classsys6' => '0',
                'classsys7' => 'nanchang',
                'classsys8' => '0',
                'classsys9' => null,
                'classsys10' => '0',
                'classsys11' => null,
                'classsys12' => null,
                'classsys13' => null,
                'classsys14' => null
            );
		}
        //标记出发地
        $this->assign('now_from_dest', $now_from_dest);
        //定义全站常量：当前出发地
        define('__FromDestID__', $now_from_dest['classsys0']); 
        //主推线路
        $topproduct = M('topproduct')->where(array('topproduct2'=>__FromDestID__))->order('topproduct4 desc')->select();
        $this->assign('topproduct', $topproduct);
        
        //热门搜索词
        $hotkeyword = M('info')->where(array('info2'=>__FromDestID__))->order('info6 desc')->select();
        $this->assign('hotkeyword', $hotkeyword);
        
		//获取最后注册的会员
		$sql="select uid,username from ".$_SC["tablepre"]."space order by uid desc limit 0,5";
		$rs=$myuser->select($sql);
		
		//获取最近发表了文章的记录
		$sql="select * from ".$_SC["tablepre"]."blog as a left join ".$_SC["tablepre"]."space as b
		      on a.uid=b.uid where a.friend=0 order by a.dateline desc limit 0,3";
		$blog=$myuser->select($sql);
		
		//获取登陆会员的信息
		
		
/****************************************************************************************/  
		//统计有多少人选择了第一个出发地
		$sql="select count(*) as cnum from ".UHDQ."userchuafadi where chuafaid=".$choosechufa[0]["classsys0"];
		$choosechufafnum=$cmr->query($sql);
		
		
		
		
		if($rs) $this->assign("userlist",$rs);
		if($blog) $this->assign("bloglist",$blog);
		
		$this->assign("choosechufa",$choosechufa);
    	$this->assign("choosechufafnum",$choosechufafnum[0]);
		
    	$this->assign("chufalist",$chufalist);
		
		return $this->fetch("public:top");
		
		
		
	}
	/**
	 * 共js用ajax方式调用，作用：远程提交社区的用户名和密码，。获取登陆状态
	 * 
	 */
	function ajaxuserlogin()
	{
		$username=$_REQUEST["username"];
		$password=$_REQUEST["password"];
		
		if(preg_replace("/[\s]*/", "", $username)=="") die("用户名不正确");
		if(preg_replace("/[\s]*/", "", $password)=="") die("密码不正确");
		$content=file_get_contents(SYS_UCHOMEURL."/api/login.php?username=".$username."&password=".$password);
		//echo preg_replace("/^[\s\S]+returnrecord\=/", "", $content);
		
		echo $content;
	
	}
	
	/**
	 * 给用户添加选定的出发地
	 */
	function addcoochechufa()
	{
		$uid=$_GET["uid"];
		$chufaid=$_GET["chufaid"];
		if(!is_numeric($uid) or !is_numeric($chufaid)) die("请输入正确的id");
		if($this->countuserchufa()>4) die("已经超出了最大数");
		$mr=new Model();
		$sql="select * from ".UHDQ."userchuafadi where uid=".$uid." chuafaid=".$chufaid;
		$rs=$this->getRsListBySql($sql);;
		if(!$rs)
		{
			$sql="insert into ".UHDQ."userchuafadi(uid,chuafaid)values($uid,$chufaid)";
			$this->executeBySql($sql);
		}
		
	}
	
	/**
	 * 更新用户所选的出发地
	 */
	function updatecoochechufa()
	{
		$uid=$_GET["uid"];//
		$chufaid=$_GET["chufaid"];//目标出发地id
		$oldchufaid=$_GET["oldchufaid"];//旧的出发地id
		if(!is_numeric($uid) or !is_numeric($chufaid) or !is_numeric($oldchufaid)) die("请输入正确的id");
		$sql="update ".UHDQ."userchuafadi set chuafaid=$chufaid where uid=$uid and chuafaid=$oldchufaid";
		$this->executeBySql($sql);
		
	}
	
/**
	 * 删除用户所选的出发地
	 */
	function delcoochechufa()
	{
		$uid=$_GET["uid"];//
		$chufaid=$_GET["chufaid"];//目标出发地id
		
		if(!is_numeric($uid) or !is_numeric($chufaid) ) die("请输入正确的id");
		$sql="delete from ".UHDQ."userchuafadi where chuafaid=$chufaid and uid=$uid ";
		$this->executeBySql($sql);
		echo "dook";
		
	}
	
	/**
	 * 获取已经选择了的个数
	 */
	function countuserchufa()
	{
		
		$sql="select count(*) as totnum from ".UHDQ."userchuafadi where uid=7";
		
		$rs=$this->getRsListBySql($sql);
		return $rs[0]["totnum"];
	}
	
	/**
	 * 统计有多少人选择了第一个出发地
	 */
	function countuserchooes()
	{
		
		$chufaid=$_GET["chufaid"];
		$sql="select count(*) as cnum from ".UHDQ."userchuafadi where chuafaid=".$chufaid;
		
		$choosechufafnum=$this->getRsListBySql($sql);
		echo $choosechufafnum[0]["cnum"];
	}
	/**
	 * 显示某个用户所选的出发地
	 */
	function showuserchufadi()
	{
		$uid=$_REQUEST["uid"];
		if(!is_numeric($uid)) die("你的用户名不正确");
		
		$sql="select * from ".UHDQ."userchuafadi as a  
		left join ".DQ."classsys as b on a.chuafaid=b.classsys0 where uid=".$uid;
		$rs=$this->getRsListBySql($sql);
		//生成返回的html代码
		$i=0;
		foreach ($rs as $k=>$v)
		{
			if($i==0)
			{
				?><item key=<?php echo $i; ?>><a href='/classsys/chufadi-cid-<?php echo $v["chuafaid"] ?>' id="chooseedlist_0_mchufa_url"><span class="bold">出发城市:</span><span id="chooseedlist_0_mchufa"><?php echo $v["classsys1"]; ?></span></a>
				</item>
			<?}
			else
			{ ?>
				<li id="chooseedlist_<?php echo $i; ?>" class="nav_bg"><a href='/classsys/chufadi-cid-<?php echo $v["classsys0"]; ?>' id="chooseedlist_<?php echo $i; ?>_mchufa_url">从<span id="chooseedlist_<?php echo $i; ?>_mchufa"><?php echo $v["classsys1"]; ?></span>出发</a> <a id="delchufa_<?php echo $i; ?>" turl="/public/delcoochechufa-uid-<?php echo $uid; ?>-chufaid-<?php echo $v["classsys0"]; ?>">-</a></li>
				
			<? 
			}
			$i++;
		}
		
	}
	
	/**
	 * 根据sql查询并返回查询的结果记录集
	 * @param  $sql
	 * @return 如果数据非法或者查询错误则返回false 否则返回查询结果数据集（同select方法）

	 */
	function getRsListBySql($sql)
	{
		$mr=new Model();
		return $mr->query($sql);
		
	}
	
		/**
	 * 根据sql更新或删除记录集
	 * @param  $sql
	 * @return 如果数据非法或者查询错误则返回false 否则返回影响的记录数

	 */
	function executeBySql($sql)
	{
		$mr=new Model();
		return $mr->execute($sql);
		
	}
	
	/**
	 * 根据sql查询并返回查询的结果记录集
	 * @param  $sql
	 * @return 如果数据非法或者查询错误则返回false 否则返回查询结果数据集（同select方法）

	 */
	function query($sql)
	{
		$mr=new Model();
		return $mr->query($sql);
		
	}
	
		/**
	 * 根据sql更新或删除记录集
	 * @param  $sql
	 * @return 如果数据非法或者查询错误则返回false 否则返回影响的记录数

	 */
	function execute($sql)
	{
		$mr=new Model();
		return $mr->execute($sql);
		
	}
	
	/**
	 * 页面底部
	 */
	function pagehelpfoot(){
		
		$infomr=A("Home/info");
		$helplist=$infomr->getpagefootlist();
		
		$this->assign("helplist",$helplist);
		//静态化页面底部
		$this->buildHtml("pagehelpfoot",RootDir."/Index/html/","public:pagehelpfoot");
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}

?>